<template>
  <div class="user-list">
    <!-- 面包屑 -->
    <el-row>
      <my-breadcrumb level1="部门管理" level2="部门列表"></my-breadcrumb>
    </el-row>
    <!-- 搜索 -->
    <el-row :gutter="20">
      <el-col :span="10">
        <el-input
          placeholder="请输入部门部分名称"
          v-model="query"
          @clear="getDepartmentListData"
          @keyup.native.enter="getDepartmentListData"
          clearable
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getDepartmentListData"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button
          type="success"
          icon="el-icon-circle-plus-outline"
          plain
          @click="dialogVisibleAdd = true"
          >添加部门</el-button
        >
      </el-col>
    </el-row>
    <!-- 分页 -->
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-table :data="tableData" style="width: 100%" size="mini" height="420">
        <el-table-column prop="id" label="部门编号" width="100">
        </el-table-column>
        <el-table-column prop="departname" label="部门名称" width="150">
        </el-table-column>
        <el-table-column prop="id" label="操作" width="140">
          <template slot-scope="scope">
            <el-row>
              <el-tooltip effect="dark" content="编辑" placement="top">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  plain
                  size="mini"
                  @click="handleEdit(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除" placement="top">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  plain
                  size="mini"
                  @click="handleDelete(scope.row)"
                ></el-button>
              </el-tooltip>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <!-- 添加 -->
    <com-add
      :dialogVisibleAdd="dialogVisibleAdd"
      @addDialogClosed="dialogVisibleAdd = false"
      @addSuccess="getDepartmentListData"
    ></com-add>
    <!-- 编辑 -->
    <com-edit
      :dialogVisibleEdit="dialogVisibleEdit"
      :currInfo="currInfo"
      @editDialogClosed="dialogVisibleEdit = false"
      @editSuccess="getDepartmentListData"
    ></com-edit>
  </div>
</template>

<script>
import MyBreadcrumb from '@/components/MyBreadcrumb'

import ComAdd from '@/views/department/components/ComAdd'
import ComEdit from '@/views/department/components/ComEdit'
export default {
  components: {
    MyBreadcrumb,
    ComAdd,
    ComEdit
  },
  data() {
    return {
      query: '',
      tableData: [],
      total: 0,
      pageSize: 5,
      pageNum: 1,
      dialogVisibleAdd: false,
      dialogVisibleEdit: false,
      dialogVisibleAuth: false,
      currInfo: null
    }
  },
  created() {
    this.getDepartmentListData()
  },
  computed: {},
  methods: {
    async getDepartmentListData() {
      const { data } = await this.$http.get('/department', {
        params: {
          query: this.query,
          pagenum: this.pageNum,
          pagesize: this.pageSize
        }
      })
      if (data.meta.status === 200) {
        const { total, departmentList } = data.data
        this.tableData = departmentList
        this.total = total
      } else {
        this.$message.warning(data.meta.msg)
      }
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      // 切换每页数据条目数量的时候，页码切回到 1
      this.pageNum = 1
      this.getDepartmentListData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pageNum = val
      this.getDepartmentListData()
    },

    // 删除按钮
    handleDelete(departmentInfo) {
      this.$confirm('此操作将删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const {
            data: { meta }
          } = await this.$http.delete('/department/' + departmentInfo.id)

          if (meta.status === 200) {
            this.$message.success(meta.msg)
            this.getDepartmentListData()
          } else {
            this.$message.error(meta.msg)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑用户
    handleEdit(departmentInfo) {
      this.dialogVisibleEdit = true
      this.currInfo = JSON.parse(JSON.stringify(departmentInfo))
    }
  }
}
</script>

<style lang="less" scoped>
.user-list {
  .el-row {
    padding: 5px 0;
  }
}
</style>
